{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "plt.rcParams[\"font.sans-serif\"] = [\"SimHei\"]\n",
    "plt.rcParams[\"axes.unicode_minus\"] = False\n",
    "pd.options.display.max_rows = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import itertools\n",
    "from collections import defaultdict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(r\"C:\\Users\\新田草\\Desktop\\正仁量化\\data\\CY1805.csv\") as f:\n",
    "    data = pd.read_csv(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>Date</th>\n",
       "      <th>Time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>LastPx</th>\n",
       "      <th>HighPx</th>\n",
       "      <th>LowPx</th>\n",
       "      <th>OpenPx</th>\n",
       "      <th>PreClosePx</th>\n",
       "      <th>TotalVolumeTrade</th>\n",
       "      <th>...</th>\n",
       "      <th>BidPx2</th>\n",
       "      <th>BidPx3</th>\n",
       "      <th>BidPx4</th>\n",
       "      <th>BidPx5</th>\n",
       "      <th>BidVol1</th>\n",
       "      <th>BidVol2</th>\n",
       "      <th>BidVol3</th>\n",
       "      <th>BidVol4</th>\n",
       "      <th>BidVol5</th>\n",
       "      <th>BuyOrSell</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90713351.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90713391.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90714298.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90717189.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90721140.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Stock        Date        Time     Timestamp   LastPx   HighPx    LowPx  \\\n",
       "0  CY1805  20180102.0  90713351.0  1.514855e+09  22800.0  22800.0  22800.0   \n",
       "1  CY1805  20180102.0  90713391.0  1.514855e+09  23205.0  23205.0  22800.0   \n",
       "2  CY1805  20180102.0  90714298.0  1.514855e+09  23205.0  23205.0  22800.0   \n",
       "3  CY1805  20180102.0  90717189.0  1.514855e+09  23205.0  23205.0  22800.0   \n",
       "4  CY1805  20180102.0  90721140.0  1.514855e+09  23205.0  23205.0  22800.0   \n",
       "\n",
       "    OpenPx  PreClosePx  TotalVolumeTrade    ...       BidPx2   BidPx3  \\\n",
       "0  22800.0     23100.0               2.0    ...      22450.0  22395.0   \n",
       "1  22800.0     23100.0               4.0    ...      22450.0  22395.0   \n",
       "2  22800.0     23100.0               4.0    ...      22450.0  22395.0   \n",
       "3  22800.0     23100.0               4.0    ...      23205.0  22450.0   \n",
       "4  22800.0     23100.0               4.0    ...      23205.0  22450.0   \n",
       "\n",
       "    BidPx4    BidPx5  BidVol1  BidVol2  BidVol3  BidVol4  BidVol5  BuyOrSell  \n",
       "0  22390.0  22389.99      1.0      1.0      1.0      1.0      0.0          B  \n",
       "1  22390.0  22389.99      4.0      1.0      1.0      1.0      0.0          B  \n",
       "2  22390.0  22389.99      4.0      1.0      1.0      1.0      0.0          N  \n",
       "3  22390.0  22389.99      1.0      4.0      1.0      1.0      0.0          N  \n",
       "4  22390.0  22389.99      1.0      4.0      1.0      1.0      0.0          N  \n",
       "\n",
       "[5 rows x 35 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27982"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>Date</th>\n",
       "      <th>Time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>LastPx</th>\n",
       "      <th>HighPx</th>\n",
       "      <th>LowPx</th>\n",
       "      <th>OpenPx</th>\n",
       "      <th>PreClosePx</th>\n",
       "      <th>TotalVolumeTrade</th>\n",
       "      <th>...</th>\n",
       "      <th>BidPx3</th>\n",
       "      <th>BidPx4</th>\n",
       "      <th>BidPx5</th>\n",
       "      <th>BidVol1</th>\n",
       "      <th>BidVol2</th>\n",
       "      <th>BidVol3</th>\n",
       "      <th>BidVol4</th>\n",
       "      <th>BidVol5</th>\n",
       "      <th>BuyOrSell</th>\n",
       "      <th>Timestamp_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90713351.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22395.00</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-01-02 09:07:13+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90713391.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22395.00</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-01-02 09:07:13+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90714298.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22395.00</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2018-01-02 09:07:14+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90717189.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.00</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2018-01-02 09:07:17+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90721140.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.00</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2018-01-02 09:07:21+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90734233.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.00</td>\n",
       "      <td>22450.00</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2018-01-02 09:07:34+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90743565.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.00</td>\n",
       "      <td>22450.00</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2018-01-02 09:07:44+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90744261.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.00</td>\n",
       "      <td>22450.00</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2018-01-02 09:07:44+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90800776.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.00</td>\n",
       "      <td>22450.00</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2018-01-02 09:08:01+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90806080.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.00</td>\n",
       "      <td>22450.00</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2018-01-02 09:08:06+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27971</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180423.0</td>\n",
       "      <td>144108038.0</td>\n",
       "      <td>1.524466e+09</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23115.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22194.99</td>\n",
       "      <td>22194.98</td>\n",
       "      <td>22194.97</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-04-23 14:41:08+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27972</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180423.0</td>\n",
       "      <td>144526324.0</td>\n",
       "      <td>1.524466e+09</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23115.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22199.98</td>\n",
       "      <td>22199.97</td>\n",
       "      <td>22199.96</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-04-23 14:45:26+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27973</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180423.0</td>\n",
       "      <td>144527273.0</td>\n",
       "      <td>1.524466e+09</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23115.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22199.99</td>\n",
       "      <td>22199.98</td>\n",
       "      <td>22199.97</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-04-23 14:45:27+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27974</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180423.0</td>\n",
       "      <td>144703629.0</td>\n",
       "      <td>1.524466e+09</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23115.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22204.98</td>\n",
       "      <td>22204.97</td>\n",
       "      <td>22204.96</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-04-23 14:47:04+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27975</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180423.0</td>\n",
       "      <td>144703808.0</td>\n",
       "      <td>1.524466e+09</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23115.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22204.99</td>\n",
       "      <td>22204.98</td>\n",
       "      <td>22204.97</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-04-23 14:47:04+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27976</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180423.0</td>\n",
       "      <td>145814872.0</td>\n",
       "      <td>1.524467e+09</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23115.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22205.00</td>\n",
       "      <td>22204.99</td>\n",
       "      <td>22204.98</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-04-23 14:58:15+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27977</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180423.0</td>\n",
       "      <td>145858267.0</td>\n",
       "      <td>1.524467e+09</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23115.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22204.99</td>\n",
       "      <td>22204.98</td>\n",
       "      <td>22204.97</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-04-23 14:58:58+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27978</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180423.0</td>\n",
       "      <td>145902437.0</td>\n",
       "      <td>1.524467e+09</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23115.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22205.00</td>\n",
       "      <td>22204.99</td>\n",
       "      <td>22204.98</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-04-23 14:59:02+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27979</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180423.0</td>\n",
       "      <td>145956468.0</td>\n",
       "      <td>1.524467e+09</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23770.0</td>\n",
       "      <td>23115.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22204.99</td>\n",
       "      <td>22204.98</td>\n",
       "      <td>22204.97</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-04-23 14:59:56+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27981</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180427.0</td>\n",
       "      <td>100124171.0</td>\n",
       "      <td>1.524794e+09</td>\n",
       "      <td>23200.0</td>\n",
       "      <td>23200.0</td>\n",
       "      <td>23200.0</td>\n",
       "      <td>23200.0</td>\n",
       "      <td>24100.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23199.98</td>\n",
       "      <td>23199.97</td>\n",
       "      <td>23199.96</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-04-27 10:01:24+08:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>27909 rows × 36 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        Stock        Date         Time     Timestamp   LastPx   HighPx  \\\n",
       "0      CY1805  20180102.0   90713351.0  1.514855e+09  22800.0  22800.0   \n",
       "1      CY1805  20180102.0   90713391.0  1.514855e+09  23205.0  23205.0   \n",
       "2      CY1805  20180102.0   90714298.0  1.514855e+09  23205.0  23205.0   \n",
       "3      CY1805  20180102.0   90717189.0  1.514855e+09  23205.0  23205.0   \n",
       "4      CY1805  20180102.0   90721140.0  1.514855e+09  23205.0  23205.0   \n",
       "5      CY1805  20180102.0   90734233.0  1.514855e+09  23205.0  23205.0   \n",
       "6      CY1805  20180102.0   90743565.0  1.514855e+09  23205.0  23205.0   \n",
       "7      CY1805  20180102.0   90744261.0  1.514855e+09  23205.0  23205.0   \n",
       "8      CY1805  20180102.0   90800776.0  1.514855e+09  23205.0  23205.0   \n",
       "9      CY1805  20180102.0   90806080.0  1.514855e+09  23205.0  23205.0   \n",
       "...       ...         ...          ...           ...      ...      ...   \n",
       "27971  CY1805  20180423.0  144108038.0  1.524466e+09  23770.0  23770.0   \n",
       "27972  CY1805  20180423.0  144526324.0  1.524466e+09  23770.0  23770.0   \n",
       "27973  CY1805  20180423.0  144527273.0  1.524466e+09  23770.0  23770.0   \n",
       "27974  CY1805  20180423.0  144703629.0  1.524466e+09  23770.0  23770.0   \n",
       "27975  CY1805  20180423.0  144703808.0  1.524466e+09  23770.0  23770.0   \n",
       "27976  CY1805  20180423.0  145814872.0  1.524467e+09  23770.0  23770.0   \n",
       "27977  CY1805  20180423.0  145858267.0  1.524467e+09  23770.0  23770.0   \n",
       "27978  CY1805  20180423.0  145902437.0  1.524467e+09  23770.0  23770.0   \n",
       "27979  CY1805  20180423.0  145956468.0  1.524467e+09  23770.0  23770.0   \n",
       "27981  CY1805  20180427.0  100124171.0  1.524794e+09  23200.0  23200.0   \n",
       "\n",
       "         LowPx   OpenPx  PreClosePx  TotalVolumeTrade  \\\n",
       "0      22800.0  22800.0     23100.0               2.0   \n",
       "1      22800.0  22800.0     23100.0               4.0   \n",
       "2      22800.0  22800.0     23100.0               4.0   \n",
       "3      22800.0  22800.0     23100.0               4.0   \n",
       "4      22800.0  22800.0     23100.0               4.0   \n",
       "5      22800.0  22800.0     23100.0               4.0   \n",
       "6      22800.0  22800.0     23100.0               4.0   \n",
       "7      22800.0  22800.0     23100.0               4.0   \n",
       "8      22800.0  22800.0     23100.0               4.0   \n",
       "9      22800.0  22800.0     23100.0               4.0   \n",
       "...        ...      ...         ...               ...   \n",
       "27971  23770.0  23770.0     23115.0               2.0   \n",
       "27972  23770.0  23770.0     23115.0               2.0   \n",
       "27973  23770.0  23770.0     23115.0               2.0   \n",
       "27974  23770.0  23770.0     23115.0               2.0   \n",
       "27975  23770.0  23770.0     23115.0               2.0   \n",
       "27976  23770.0  23770.0     23115.0               2.0   \n",
       "27977  23770.0  23770.0     23115.0               2.0   \n",
       "27978  23770.0  23770.0     23115.0               2.0   \n",
       "27979  23770.0  23770.0     23115.0               2.0   \n",
       "27981  23200.0  23200.0     24100.0               2.0   \n",
       "\n",
       "                 ...               BidPx3    BidPx4    BidPx5  BidVol1  \\\n",
       "0                ...             22395.00  22390.00  22389.99      1.0   \n",
       "1                ...             22395.00  22390.00  22389.99      4.0   \n",
       "2                ...             22395.00  22390.00  22389.99      4.0   \n",
       "3                ...             22450.00  22390.00  22389.99      1.0   \n",
       "4                ...             22450.00  22390.00  22389.99      1.0   \n",
       "5                ...             23205.00  22450.00  22390.00      1.0   \n",
       "6                ...             23205.00  22450.00  22390.00      1.0   \n",
       "7                ...             23205.00  22450.00  22390.00      1.0   \n",
       "8                ...             23205.00  22450.00  22390.00      1.0   \n",
       "9                ...             23205.00  22450.00  22390.00      1.0   \n",
       "...              ...                  ...       ...       ...      ...   \n",
       "27971            ...             22194.99  22194.98  22194.97      1.0   \n",
       "27972            ...             22199.98  22199.97  22199.96      1.0   \n",
       "27973            ...             22199.99  22199.98  22199.97      1.0   \n",
       "27974            ...             22204.98  22204.97  22204.96      1.0   \n",
       "27975            ...             22204.99  22204.98  22204.97      1.0   \n",
       "27976            ...             22205.00  22204.99  22204.98      2.0   \n",
       "27977            ...             22204.99  22204.98  22204.97      2.0   \n",
       "27978            ...             22205.00  22204.99  22204.98      1.0   \n",
       "27979            ...             22204.99  22204.98  22204.97      2.0   \n",
       "27981            ...             23199.98  23199.97  23199.96      1.0   \n",
       "\n",
       "       BidVol2  BidVol3  BidVol4  BidVol5  BuyOrSell            Timestamp_time  \n",
       "0          1.0      1.0      1.0      0.0          B 2018-01-02 09:07:13+08:00  \n",
       "1          1.0      1.0      1.0      0.0          B 2018-01-02 09:07:13+08:00  \n",
       "2          1.0      1.0      1.0      0.0          N 2018-01-02 09:07:14+08:00  \n",
       "3          4.0      1.0      1.0      0.0          N 2018-01-02 09:07:17+08:00  \n",
       "4          4.0      1.0      1.0      0.0          N 2018-01-02 09:07:21+08:00  \n",
       "5          1.0      4.0      1.0      1.0          N 2018-01-02 09:07:34+08:00  \n",
       "6          1.0      4.0      1.0      1.0          N 2018-01-02 09:07:44+08:00  \n",
       "7          1.0      4.0      1.0      1.0          N 2018-01-02 09:07:44+08:00  \n",
       "8          1.0      4.0      1.0      1.0          N 2018-01-02 09:08:01+08:00  \n",
       "9          1.0      4.0      1.0      1.0          N 2018-01-02 09:08:06+08:00  \n",
       "...        ...      ...      ...      ...        ...                       ...  \n",
       "27971      1.0      0.0      0.0      0.0          B 2018-04-23 14:41:08+08:00  \n",
       "27972      0.0      0.0      0.0      0.0          B 2018-04-23 14:45:26+08:00  \n",
       "27973      1.0      0.0      0.0      0.0          B 2018-04-23 14:45:27+08:00  \n",
       "27974      0.0      0.0      0.0      0.0          B 2018-04-23 14:47:04+08:00  \n",
       "27975      1.0      0.0      0.0      0.0          B 2018-04-23 14:47:04+08:00  \n",
       "27976      1.0      1.0      0.0      0.0          B 2018-04-23 14:58:15+08:00  \n",
       "27977      1.0      0.0      0.0      0.0          B 2018-04-23 14:58:58+08:00  \n",
       "27978      2.0      1.0      0.0      0.0          B 2018-04-23 14:59:02+08:00  \n",
       "27979      1.0      0.0      0.0      0.0          B 2018-04-23 14:59:56+08:00  \n",
       "27981      0.0      0.0      0.0      0.0          B 2018-04-27 10:01:24+08:00  \n",
       "\n",
       "[27909 rows x 36 columns]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>Date</th>\n",
       "      <th>Time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>LastPx</th>\n",
       "      <th>HighPx</th>\n",
       "      <th>LowPx</th>\n",
       "      <th>OpenPx</th>\n",
       "      <th>PreClosePx</th>\n",
       "      <th>TotalVolumeTrade</th>\n",
       "      <th>...</th>\n",
       "      <th>BidPx2</th>\n",
       "      <th>BidPx3</th>\n",
       "      <th>BidPx4</th>\n",
       "      <th>BidPx5</th>\n",
       "      <th>BidVol1</th>\n",
       "      <th>BidVol2</th>\n",
       "      <th>BidVol3</th>\n",
       "      <th>BidVol4</th>\n",
       "      <th>BidVol5</th>\n",
       "      <th>BuyOrSell</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90713351.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90713391.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90714298.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90717189.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90721140.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Stock        Date        Time     Timestamp   LastPx   HighPx    LowPx  \\\n",
       "0  CY1805  20180102.0  90713351.0  1.514855e+09  22800.0  22800.0  22800.0   \n",
       "1  CY1805  20180102.0  90713391.0  1.514855e+09  23205.0  23205.0  22800.0   \n",
       "2  CY1805  20180102.0  90714298.0  1.514855e+09  23205.0  23205.0  22800.0   \n",
       "3  CY1805  20180102.0  90717189.0  1.514855e+09  23205.0  23205.0  22800.0   \n",
       "4  CY1805  20180102.0  90721140.0  1.514855e+09  23205.0  23205.0  22800.0   \n",
       "\n",
       "    OpenPx  PreClosePx  TotalVolumeTrade    ...       BidPx2   BidPx3  \\\n",
       "0  22800.0     23100.0               2.0    ...      22450.0  22395.0   \n",
       "1  22800.0     23100.0               4.0    ...      22450.0  22395.0   \n",
       "2  22800.0     23100.0               4.0    ...      22450.0  22395.0   \n",
       "3  22800.0     23100.0               4.0    ...      23205.0  22450.0   \n",
       "4  22800.0     23100.0               4.0    ...      23205.0  22450.0   \n",
       "\n",
       "    BidPx4    BidPx5  BidVol1  BidVol2  BidVol3  BidVol4  BidVol5  BuyOrSell  \n",
       "0  22390.0  22389.99      1.0      1.0      1.0      1.0      0.0          B  \n",
       "1  22390.0  22389.99      4.0      1.0      1.0      1.0      0.0          B  \n",
       "2  22390.0  22389.99      4.0      1.0      1.0      1.0      0.0          N  \n",
       "3  22390.0  22389.99      1.0      4.0      1.0      1.0      0.0          N  \n",
       "4  22390.0  22389.99      1.0      4.0      1.0      1.0      0.0          N  \n",
       "\n",
       "[5 rows x 35 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data = data.iloc[:2000, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2000, 35)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>Date</th>\n",
       "      <th>Time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>LastPx</th>\n",
       "      <th>HighPx</th>\n",
       "      <th>LowPx</th>\n",
       "      <th>OpenPx</th>\n",
       "      <th>PreClosePx</th>\n",
       "      <th>TotalVolumeTrade</th>\n",
       "      <th>...</th>\n",
       "      <th>BidPx2</th>\n",
       "      <th>BidPx3</th>\n",
       "      <th>BidPx4</th>\n",
       "      <th>BidPx5</th>\n",
       "      <th>BidVol1</th>\n",
       "      <th>BidVol2</th>\n",
       "      <th>BidVol3</th>\n",
       "      <th>BidVol4</th>\n",
       "      <th>BidVol5</th>\n",
       "      <th>BuyOrSell</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90713351.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90713391.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90714298.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90717189.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90721140.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Stock        Date        Time     Timestamp   LastPx   HighPx    LowPx  \\\n",
       "0  CY1805  20180102.0  90713351.0  1.514855e+09  22800.0  22800.0  22800.0   \n",
       "1  CY1805  20180102.0  90713391.0  1.514855e+09  23205.0  23205.0  22800.0   \n",
       "2  CY1805  20180102.0  90714298.0  1.514855e+09  23205.0  23205.0  22800.0   \n",
       "3  CY1805  20180102.0  90717189.0  1.514855e+09  23205.0  23205.0  22800.0   \n",
       "4  CY1805  20180102.0  90721140.0  1.514855e+09  23205.0  23205.0  22800.0   \n",
       "\n",
       "    OpenPx  PreClosePx  TotalVolumeTrade    ...       BidPx2   BidPx3  \\\n",
       "0  22800.0     23100.0               2.0    ...      22450.0  22395.0   \n",
       "1  22800.0     23100.0               4.0    ...      22450.0  22395.0   \n",
       "2  22800.0     23100.0               4.0    ...      22450.0  22395.0   \n",
       "3  22800.0     23100.0               4.0    ...      23205.0  22450.0   \n",
       "4  22800.0     23100.0               4.0    ...      23205.0  22450.0   \n",
       "\n",
       "    BidPx4    BidPx5  BidVol1  BidVol2  BidVol3  BidVol4  BidVol5  BuyOrSell  \n",
       "0  22390.0  22389.99      1.0      1.0      1.0      1.0      0.0          B  \n",
       "1  22390.0  22389.99      4.0      1.0      1.0      1.0      0.0          B  \n",
       "2  22390.0  22389.99      4.0      1.0      1.0      1.0      0.0          N  \n",
       "3  22390.0  22389.99      1.0      4.0      1.0      1.0      0.0          N  \n",
       "4  22390.0  22389.99      1.0      4.0      1.0      1.0      0.0          N  \n",
       "\n",
       "[5 rows x 35 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "times = pd.to_datetime(test_data[\"Timestamp\"], unit=\"s\")\n",
    "times_index = pd.Index(times)\n",
    "times_index_Shanghai = times_index.tz_localize('UTC').tz_convert('Asia/Shanghai')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-01-02 09:07:13+08:00', '2018-01-02 09:07:13+08:00',\n",
       "               '2018-01-02 09:07:14+08:00', '2018-01-02 09:07:17+08:00',\n",
       "               '2018-01-02 09:07:21+08:00', '2018-01-02 09:07:34+08:00',\n",
       "               '2018-01-02 09:07:44+08:00', '2018-01-02 09:07:44+08:00',\n",
       "               '2018-01-02 09:08:01+08:00', '2018-01-02 09:08:06+08:00',\n",
       "               ...\n",
       "               '2018-01-02 22:39:07+08:00', '2018-01-02 22:39:12+08:00',\n",
       "               '2018-01-02 22:39:28+08:00', '2018-01-02 22:39:30+08:00',\n",
       "               '2018-01-02 22:39:33+08:00', '2018-01-02 22:39:36+08:00',\n",
       "               '2018-01-02 22:39:39+08:00', '2018-01-02 22:39:41+08:00',\n",
       "               '2018-01-02 22:39:42+08:00', '2018-01-02 22:39:44+08:00'],\n",
       "              dtype='datetime64[ns, Asia/Shanghai]', name='Timestamp', length=2000, freq=None)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "times_index_Shanghai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data = test_data.set_index([times_index_Shanghai])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>Date</th>\n",
       "      <th>Time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>LastPx</th>\n",
       "      <th>HighPx</th>\n",
       "      <th>LowPx</th>\n",
       "      <th>OpenPx</th>\n",
       "      <th>PreClosePx</th>\n",
       "      <th>TotalVolumeTrade</th>\n",
       "      <th>...</th>\n",
       "      <th>BidPx2</th>\n",
       "      <th>BidPx3</th>\n",
       "      <th>BidPx4</th>\n",
       "      <th>BidPx5</th>\n",
       "      <th>BidVol1</th>\n",
       "      <th>BidVol2</th>\n",
       "      <th>BidVol3</th>\n",
       "      <th>BidVol4</th>\n",
       "      <th>BidVol5</th>\n",
       "      <th>BuyOrSell</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:13+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90713351.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:13+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90713391.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:14+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90714298.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:17+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90717189.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:21+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90721140.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:34+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90734233.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23210.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:44+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90743565.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23210.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:44+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90744261.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23210.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:08:01+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90800776.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23210.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:08:06+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90806080.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23210.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            Stock        Date        Time     Timestamp  \\\n",
       "Timestamp                                                                 \n",
       "2018-01-02 09:07:13+08:00  CY1805  20180102.0  90713351.0  1.514855e+09   \n",
       "2018-01-02 09:07:13+08:00  CY1805  20180102.0  90713391.0  1.514855e+09   \n",
       "2018-01-02 09:07:14+08:00  CY1805  20180102.0  90714298.0  1.514855e+09   \n",
       "2018-01-02 09:07:17+08:00  CY1805  20180102.0  90717189.0  1.514855e+09   \n",
       "2018-01-02 09:07:21+08:00  CY1805  20180102.0  90721140.0  1.514855e+09   \n",
       "2018-01-02 09:07:34+08:00  CY1805  20180102.0  90734233.0  1.514855e+09   \n",
       "2018-01-02 09:07:44+08:00  CY1805  20180102.0  90743565.0  1.514855e+09   \n",
       "2018-01-02 09:07:44+08:00  CY1805  20180102.0  90744261.0  1.514855e+09   \n",
       "2018-01-02 09:08:01+08:00  CY1805  20180102.0  90800776.0  1.514855e+09   \n",
       "2018-01-02 09:08:06+08:00  CY1805  20180102.0  90806080.0  1.514855e+09   \n",
       "\n",
       "                            LastPx   HighPx    LowPx   OpenPx  PreClosePx  \\\n",
       "Timestamp                                                                   \n",
       "2018-01-02 09:07:13+08:00  22800.0  22800.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:07:13+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:07:14+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:07:17+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:07:21+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:07:34+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:07:44+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:07:44+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:08:01+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:08:06+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "\n",
       "                           TotalVolumeTrade    ...       BidPx2   BidPx3  \\\n",
       "Timestamp                                      ...                         \n",
       "2018-01-02 09:07:13+08:00               2.0    ...      22450.0  22395.0   \n",
       "2018-01-02 09:07:13+08:00               4.0    ...      22450.0  22395.0   \n",
       "2018-01-02 09:07:14+08:00               4.0    ...      22450.0  22395.0   \n",
       "2018-01-02 09:07:17+08:00               4.0    ...      23205.0  22450.0   \n",
       "2018-01-02 09:07:21+08:00               4.0    ...      23205.0  22450.0   \n",
       "2018-01-02 09:07:34+08:00               4.0    ...      23210.0  23205.0   \n",
       "2018-01-02 09:07:44+08:00               4.0    ...      23210.0  23205.0   \n",
       "2018-01-02 09:07:44+08:00               4.0    ...      23210.0  23205.0   \n",
       "2018-01-02 09:08:01+08:00               4.0    ...      23210.0  23205.0   \n",
       "2018-01-02 09:08:06+08:00               4.0    ...      23210.0  23205.0   \n",
       "\n",
       "                            BidPx4    BidPx5  BidVol1  BidVol2  BidVol3  \\\n",
       "Timestamp                                                                 \n",
       "2018-01-02 09:07:13+08:00  22390.0  22389.99      1.0      1.0      1.0   \n",
       "2018-01-02 09:07:13+08:00  22390.0  22389.99      4.0      1.0      1.0   \n",
       "2018-01-02 09:07:14+08:00  22390.0  22389.99      4.0      1.0      1.0   \n",
       "2018-01-02 09:07:17+08:00  22390.0  22389.99      1.0      4.0      1.0   \n",
       "2018-01-02 09:07:21+08:00  22390.0  22389.99      1.0      4.0      1.0   \n",
       "2018-01-02 09:07:34+08:00  22450.0  22390.00      1.0      1.0      4.0   \n",
       "2018-01-02 09:07:44+08:00  22450.0  22390.00      1.0      1.0      4.0   \n",
       "2018-01-02 09:07:44+08:00  22450.0  22390.00      1.0      1.0      4.0   \n",
       "2018-01-02 09:08:01+08:00  22450.0  22390.00      1.0      1.0      4.0   \n",
       "2018-01-02 09:08:06+08:00  22450.0  22390.00      1.0      1.0      4.0   \n",
       "\n",
       "                           BidVol4  BidVol5  BuyOrSell  \n",
       "Timestamp                                               \n",
       "2018-01-02 09:07:13+08:00      1.0      0.0          B  \n",
       "2018-01-02 09:07:13+08:00      1.0      0.0          B  \n",
       "2018-01-02 09:07:14+08:00      1.0      0.0          N  \n",
       "2018-01-02 09:07:17+08:00      1.0      0.0          N  \n",
       "2018-01-02 09:07:21+08:00      1.0      0.0          N  \n",
       "2018-01-02 09:07:34+08:00      1.0      1.0          N  \n",
       "2018-01-02 09:07:44+08:00      1.0      1.0          N  \n",
       "2018-01-02 09:07:44+08:00      1.0      1.0          N  \n",
       "2018-01-02 09:08:01+08:00      1.0      1.0          N  \n",
       "2018-01-02 09:08:06+08:00      1.0      1.0          N  \n",
       "\n",
       "[10 rows x 35 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data.to_csv(\"test_data.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_cols = data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Stock', 'Date', 'Time', 'Timestamp', 'LastPx', 'HighPx', 'LowPx',\n",
       "       'OpenPx', 'PreClosePx', 'TotalVolumeTrade', 'TotalValueTrade',\n",
       "       'TickVolumeTrade', 'TickValueTrade', 'Position', 'AskPx5', 'AskPx4',\n",
       "       'AskPx3', 'AskPx2', 'AskPx1', 'AskVol5', 'AskVol4', 'AskVol3',\n",
       "       'AskVol2', 'AskVol1', 'BidPx1', 'BidPx2', 'BidPx3', 'BidPx4', 'BidPx5',\n",
       "       'BidVol1', 'BidVol2', 'BidVol3', 'BidVol4', 'BidVol5', 'BuyOrSell'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "for ind, each in enumerate(data_cols):\n",
    "    if each == \"Date\":\n",
    "        Date_ind = ind\n",
    "    if each == \"Time\":\n",
    "        Time_ind = ind\n",
    "    if each == \"Timestamp \":\n",
    "        Timestamp_ind = ind\n",
    "    if each == \"LastPx\":\n",
    "        LastPx_ind = ind\n",
    "    if each == \"TickVolumeTrade\":\n",
    "        TickVolumeTrade_ind = ind\n",
    "    if each == \"TickValueTrade\":\n",
    "        TickValueTrade_ind = ind\n",
    "    if each == \"AskPx1\":\n",
    "        AskPx1_ind = ind\n",
    "    if each == \"AskVol1\":\n",
    "        AskVol1_ind = ind\n",
    "    if each == \"BidPx1\":\n",
    "        BidPx1_ind = ind\n",
    "    if each == \"BidVol1\":\n",
    "        BidVol1_ind = ind"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>Date</th>\n",
       "      <th>Time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>LastPx</th>\n",
       "      <th>HighPx</th>\n",
       "      <th>LowPx</th>\n",
       "      <th>OpenPx</th>\n",
       "      <th>PreClosePx</th>\n",
       "      <th>TotalVolumeTrade</th>\n",
       "      <th>...</th>\n",
       "      <th>BidPx2</th>\n",
       "      <th>BidPx3</th>\n",
       "      <th>BidPx4</th>\n",
       "      <th>BidPx5</th>\n",
       "      <th>BidVol1</th>\n",
       "      <th>BidVol2</th>\n",
       "      <th>BidVol3</th>\n",
       "      <th>BidVol4</th>\n",
       "      <th>BidVol5</th>\n",
       "      <th>BuyOrSell</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:13+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90713351.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:13+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90713391.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:14+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90714298.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22395.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:17+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90717189.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-02 09:07:21+08:00</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102.0</td>\n",
       "      <td>90721140.0</td>\n",
       "      <td>1.514855e+09</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>22800.0</td>\n",
       "      <td>23100.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>22450.0</td>\n",
       "      <td>22390.0</td>\n",
       "      <td>22389.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            Stock        Date        Time     Timestamp  \\\n",
       "Timestamp                                                                 \n",
       "2018-01-02 09:07:13+08:00  CY1805  20180102.0  90713351.0  1.514855e+09   \n",
       "2018-01-02 09:07:13+08:00  CY1805  20180102.0  90713391.0  1.514855e+09   \n",
       "2018-01-02 09:07:14+08:00  CY1805  20180102.0  90714298.0  1.514855e+09   \n",
       "2018-01-02 09:07:17+08:00  CY1805  20180102.0  90717189.0  1.514855e+09   \n",
       "2018-01-02 09:07:21+08:00  CY1805  20180102.0  90721140.0  1.514855e+09   \n",
       "\n",
       "                            LastPx   HighPx    LowPx   OpenPx  PreClosePx  \\\n",
       "Timestamp                                                                   \n",
       "2018-01-02 09:07:13+08:00  22800.0  22800.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:07:13+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:07:14+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:07:17+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "2018-01-02 09:07:21+08:00  23205.0  23205.0  22800.0  22800.0     23100.0   \n",
       "\n",
       "                           TotalVolumeTrade    ...       BidPx2   BidPx3  \\\n",
       "Timestamp                                      ...                         \n",
       "2018-01-02 09:07:13+08:00               2.0    ...      22450.0  22395.0   \n",
       "2018-01-02 09:07:13+08:00               4.0    ...      22450.0  22395.0   \n",
       "2018-01-02 09:07:14+08:00               4.0    ...      22450.0  22395.0   \n",
       "2018-01-02 09:07:17+08:00               4.0    ...      23205.0  22450.0   \n",
       "2018-01-02 09:07:21+08:00               4.0    ...      23205.0  22450.0   \n",
       "\n",
       "                            BidPx4    BidPx5  BidVol1  BidVol2  BidVol3  \\\n",
       "Timestamp                                                                 \n",
       "2018-01-02 09:07:13+08:00  22390.0  22389.99      1.0      1.0      1.0   \n",
       "2018-01-02 09:07:13+08:00  22390.0  22389.99      4.0      1.0      1.0   \n",
       "2018-01-02 09:07:14+08:00  22390.0  22389.99      4.0      1.0      1.0   \n",
       "2018-01-02 09:07:17+08:00  22390.0  22389.99      1.0      4.0      1.0   \n",
       "2018-01-02 09:07:21+08:00  22390.0  22389.99      1.0      4.0      1.0   \n",
       "\n",
       "                           BidVol4  BidVol5  BuyOrSell  \n",
       "Timestamp                                               \n",
       "2018-01-02 09:07:13+08:00      1.0      0.0          B  \n",
       "2018-01-02 09:07:13+08:00      1.0      0.0          B  \n",
       "2018-01-02 09:07:14+08:00      1.0      0.0          N  \n",
       "2018-01-02 09:07:17+08:00      1.0      0.0          N  \n",
       "2018-01-02 09:07:21+08:00      1.0      0.0          N  \n",
       "\n",
       "[5 rows x 35 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data_values = test_data.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['CY1805', 20180102.0, 90713351.0, ..., 1.0, 0.0, 'B'],\n",
       "       ['CY1805', 20180102.0, 90713391.0, ..., 1.0, 0.0, 'B'],\n",
       "       ['CY1805', 20180102.0, 90714298.0, ..., 1.0, 0.0, 'N'],\n",
       "       ...,\n",
       "       ['CY1805', 20180102.0, 223941257.0, ..., 1.0, 1.0, 'N'],\n",
       "       ['CY1805', 20180102.0, 223942280.0, ..., 1.0, 1.0, 'N'],\n",
       "       ['CY1805', 20180102.0, 223944378.0, ..., 1.0, 1.0, 'N']],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data_values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "askPrice5 = test_data_values[:, AskPx1_ind-4]\n",
    "bidPrice5 = test_data_values[:, BidPx1_ind+4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "TickVolumeTrade = test_data_values[:, TickVolumeTrade_ind]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0    1970\n",
       "2.0      21\n",
       "4.0       7\n",
       "Name: TickVolumeTrade, dtype: int64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data.TickVolumeTrade.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0     27448\n",
       "2.0       414\n",
       "4.0        41\n",
       "6.0         4\n",
       "10.0        1\n",
       "12.0        1\n",
       "Name: TickVolumeTrade, dtype: int64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.TickVolumeTrade.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Float64Index([0.0, 2.0, 4.0, 6.0, 10.0, 12.0], dtype='float64')"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.TickVolumeTrade.value_counts().index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([27448,   414,    41,     4,     1,     1], dtype=int64)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.TickVolumeTrade.value_counts().values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "times = pd.to_datetime(data[\"Timestamp\"], unit=\"s\")\n",
    "times_index = pd.Index(times)\n",
    "times_index_Shanghai = times_index.tz_localize('UTC').tz_convert('Asia/Shanghai')\n",
    "data[\"Timestamp_time\"] = times_index_Shanghai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>Date</th>\n",
       "      <th>Time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>LastPx</th>\n",
       "      <th>HighPx</th>\n",
       "      <th>LowPx</th>\n",
       "      <th>OpenPx</th>\n",
       "      <th>PreClosePx</th>\n",
       "      <th>TotalVolumeTrade</th>\n",
       "      <th>...</th>\n",
       "      <th>BidPx3</th>\n",
       "      <th>BidPx4</th>\n",
       "      <th>BidPx5</th>\n",
       "      <th>BidVol1</th>\n",
       "      <th>BidVol2</th>\n",
       "      <th>BidVol3</th>\n",
       "      <th>BidVol4</th>\n",
       "      <th>BidVol5</th>\n",
       "      <th>BuyOrSell</th>\n",
       "      <th>Timestamp_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>16234</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180209.0</td>\n",
       "      <td>230202195.0</td>\n",
       "      <td>1.518189e+09</td>\n",
       "      <td>23350.0</td>\n",
       "      <td>23750.0</td>\n",
       "      <td>22575.0</td>\n",
       "      <td>23345.0</td>\n",
       "      <td>23175.0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22630.0</td>\n",
       "      <td>22600.0</td>\n",
       "      <td>22580.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2018-02-09 23:02:02+08:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 36 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        Stock        Date         Time     Timestamp   LastPx   HighPx  \\\n",
       "16234  CY1805  20180209.0  230202195.0  1.518189e+09  23350.0  23750.0   \n",
       "\n",
       "         LowPx   OpenPx  PreClosePx  TotalVolumeTrade  \\\n",
       "16234  22575.0  23345.0     23175.0              32.0   \n",
       "\n",
       "                 ...              BidPx3   BidPx4   BidPx5  BidVol1  BidVol2  \\\n",
       "16234            ...             22630.0  22600.0  22580.0      1.0      2.0   \n",
       "\n",
       "       BidVol3  BidVol4  BidVol5  BuyOrSell            Timestamp_time  \n",
       "16234      1.0      1.0      1.0          N 2018-02-09 23:02:02+08:00  \n",
       "\n",
       "[1 rows x 36 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[data[\"TickVolumeTrade\"] == 12]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.iloc[16230: 16240, :].to_csv(\"test.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>Date</th>\n",
       "      <th>Time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>LastPx</th>\n",
       "      <th>HighPx</th>\n",
       "      <th>LowPx</th>\n",
       "      <th>OpenPx</th>\n",
       "      <th>PreClosePx</th>\n",
       "      <th>TotalVolumeTrade</th>\n",
       "      <th>...</th>\n",
       "      <th>BidPx3</th>\n",
       "      <th>BidPx4</th>\n",
       "      <th>BidPx5</th>\n",
       "      <th>BidVol1</th>\n",
       "      <th>BidVol2</th>\n",
       "      <th>BidVol3</th>\n",
       "      <th>BidVol4</th>\n",
       "      <th>BidVol5</th>\n",
       "      <th>BuyOrSell</th>\n",
       "      <th>Timestamp_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3756</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180108.0</td>\n",
       "      <td>92415082.0</td>\n",
       "      <td>1.515375e+09</td>\n",
       "      <td>23015.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23015.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23155.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22570.0</td>\n",
       "      <td>22510.0</td>\n",
       "      <td>22495.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-01-08 09:24:15+08:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 36 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       Stock        Date        Time     Timestamp   LastPx   HighPx    LowPx  \\\n",
       "3756  CY1805  20180108.0  92415082.0  1.515375e+09  23015.0  23205.0  23015.0   \n",
       "\n",
       "       OpenPx  PreClosePx  TotalVolumeTrade            ...             \\\n",
       "3756  23205.0     23155.0              38.0            ...              \n",
       "\n",
       "       BidPx3   BidPx4   BidPx5  BidVol1  BidVol2  BidVol3  BidVol4  BidVol5  \\\n",
       "3756  22570.0  22510.0  22495.0      1.0      1.0      1.0      1.0      1.0   \n",
       "\n",
       "      BuyOrSell            Timestamp_time  \n",
       "3756          B 2018-01-08 09:24:15+08:00  \n",
       "\n",
       "[1 rows x 36 columns]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[data[\"TickVolumeTrade\"] == 10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.iloc[3750: 3760, :].to_csv(\"test10.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>Date</th>\n",
       "      <th>Time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>LastPx</th>\n",
       "      <th>HighPx</th>\n",
       "      <th>LowPx</th>\n",
       "      <th>OpenPx</th>\n",
       "      <th>PreClosePx</th>\n",
       "      <th>TotalVolumeTrade</th>\n",
       "      <th>...</th>\n",
       "      <th>BidPx3</th>\n",
       "      <th>BidPx4</th>\n",
       "      <th>BidPx5</th>\n",
       "      <th>BidVol1</th>\n",
       "      <th>BidVol2</th>\n",
       "      <th>BidVol3</th>\n",
       "      <th>BidVol4</th>\n",
       "      <th>BidVol5</th>\n",
       "      <th>BuyOrSell</th>\n",
       "      <th>Timestamp_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3736</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180108.0</td>\n",
       "      <td>90134009.0</td>\n",
       "      <td>1.515373e+09</td>\n",
       "      <td>23065.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23015.0</td>\n",
       "      <td>23205.0</td>\n",
       "      <td>23155.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22810.00</td>\n",
       "      <td>22570.00</td>\n",
       "      <td>22510.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-01-08 09:01:34+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15172</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180208.0</td>\n",
       "      <td>144046072.0</td>\n",
       "      <td>1.518072e+09</td>\n",
       "      <td>23090.0</td>\n",
       "      <td>23160.0</td>\n",
       "      <td>23090.0</td>\n",
       "      <td>23160.0</td>\n",
       "      <td>23250.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22500.00</td>\n",
       "      <td>22320.00</td>\n",
       "      <td>22319.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-02-08 14:40:46+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18217</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180214.0</td>\n",
       "      <td>140706276.0</td>\n",
       "      <td>1.518588e+09</td>\n",
       "      <td>23350.0</td>\n",
       "      <td>23350.0</td>\n",
       "      <td>22915.0</td>\n",
       "      <td>22915.0</td>\n",
       "      <td>23080.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22170.00</td>\n",
       "      <td>22169.99</td>\n",
       "      <td>22169.98</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-02-14 14:07:06+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27399</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180418.0</td>\n",
       "      <td>112618348.0</td>\n",
       "      <td>1.524022e+09</td>\n",
       "      <td>23375.0</td>\n",
       "      <td>23375.0</td>\n",
       "      <td>23375.0</td>\n",
       "      <td>23375.0</td>\n",
       "      <td>23280.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23079.99</td>\n",
       "      <td>23079.98</td>\n",
       "      <td>23079.97</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-04-18 11:26:18+08:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4 rows × 36 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        Stock        Date         Time     Timestamp   LastPx   HighPx  \\\n",
       "3736   CY1805  20180108.0   90134009.0  1.515373e+09  23065.0  23205.0   \n",
       "15172  CY1805  20180208.0  144046072.0  1.518072e+09  23090.0  23160.0   \n",
       "18217  CY1805  20180214.0  140706276.0  1.518588e+09  23350.0  23350.0   \n",
       "27399  CY1805  20180418.0  112618348.0  1.524022e+09  23375.0  23375.0   \n",
       "\n",
       "         LowPx   OpenPx  PreClosePx  TotalVolumeTrade  \\\n",
       "3736   23015.0  23205.0     23155.0              26.0   \n",
       "15172  23090.0  23160.0     23250.0               6.0   \n",
       "18217  22915.0  22915.0     23080.0              12.0   \n",
       "27399  23375.0  23375.0     23280.0               6.0   \n",
       "\n",
       "                 ...               BidPx3    BidPx4    BidPx5  BidVol1  \\\n",
       "3736             ...             22810.00  22570.00  22510.00      1.0   \n",
       "15172            ...             22500.00  22320.00  22319.99      1.0   \n",
       "18217            ...             22170.00  22169.99  22169.98      1.0   \n",
       "27399            ...             23079.99  23079.98  23079.97      2.0   \n",
       "\n",
       "       BidVol2  BidVol3  BidVol4  BidVol5  BuyOrSell            Timestamp_time  \n",
       "3736       1.0      1.0      1.0      1.0          B 2018-01-08 09:01:34+08:00  \n",
       "15172      1.0      2.0      5.0      0.0          B 2018-02-08 14:40:46+08:00  \n",
       "18217      3.0      2.0      0.0      0.0          B 2018-02-14 14:07:06+08:00  \n",
       "27399      1.0      0.0      0.0      0.0          B 2018-04-18 11:26:18+08:00  \n",
       "\n",
       "[4 rows x 36 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[data[\"TickVolumeTrade\"] == 6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.iloc[27390: 27405, :].to_csv(\"test64.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>Date</th>\n",
       "      <th>Time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>LastPx</th>\n",
       "      <th>HighPx</th>\n",
       "      <th>LowPx</th>\n",
       "      <th>OpenPx</th>\n",
       "      <th>PreClosePx</th>\n",
       "      <th>TotalVolumeTrade</th>\n",
       "      <th>...</th>\n",
       "      <th>BidPx2</th>\n",
       "      <th>BidPx3</th>\n",
       "      <th>BidPx4</th>\n",
       "      <th>BidPx5</th>\n",
       "      <th>BidVol1</th>\n",
       "      <th>BidVol2</th>\n",
       "      <th>BidVol3</th>\n",
       "      <th>BidVol4</th>\n",
       "      <th>BidVol5</th>\n",
       "      <th>BuyOrSell</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>16234</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180209.0</td>\n",
       "      <td>230202195.0</td>\n",
       "      <td>1.518189e+09</td>\n",
       "      <td>23350.0</td>\n",
       "      <td>23750.0</td>\n",
       "      <td>22575.0</td>\n",
       "      <td>23345.0</td>\n",
       "      <td>23175.0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22635.0</td>\n",
       "      <td>22630.0</td>\n",
       "      <td>22600.0</td>\n",
       "      <td>22580.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        Stock        Date         Time     Timestamp   LastPx   HighPx  \\\n",
       "16234  CY1805  20180209.0  230202195.0  1.518189e+09  23350.0  23750.0   \n",
       "\n",
       "         LowPx   OpenPx  PreClosePx  TotalVolumeTrade    ...       BidPx2  \\\n",
       "16234  22575.0  23345.0     23175.0              32.0    ...      22635.0   \n",
       "\n",
       "        BidPx3   BidPx4   BidPx5  BidVol1  BidVol2  BidVol3  BidVol4  BidVol5  \\\n",
       "16234  22630.0  22600.0  22580.0      1.0      2.0      1.0      1.0      1.0   \n",
       "\n",
       "       BuyOrSell  \n",
       "16234          N  \n",
       "\n",
       "[1 rows x 35 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[data[\"TickVolumeTrade\"] == 12]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.iloc[16230: 16240, :].to_csv(\"test12.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0     27448\n",
       "2.0       414\n",
       "4.0        41\n",
       "6.0         4\n",
       "10.0        1\n",
       "12.0        1\n",
       "Name: TickVolumeTrade, dtype: int64"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.TickVolumeTrade.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Float64Index([0.0, 2.0, 4.0, 6.0, 10.0, 12.0], dtype='float64')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.TickVolumeTrade.value_counts().index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Stock', 'Date', 'Time', 'Timestamp', 'LastPx', 'HighPx', 'LowPx',\n",
       "       'OpenPx', 'PreClosePx', 'TotalVolumeTrade', 'TotalValueTrade',\n",
       "       'TickVolumeTrade', 'TickValueTrade', 'Position', 'AskPx5', 'AskPx4',\n",
       "       'AskPx3', 'AskPx2', 'AskPx1', 'AskVol5', 'AskVol4', 'AskVol3',\n",
       "       'AskVol2', 'AskVol1', 'BidPx1', 'BidPx2', 'BidPx3', 'BidPx4', 'BidPx5',\n",
       "       'BidVol1', 'BidVol2', 'BidVol3', 'BidVol4', 'BidVol5', 'BuyOrSell'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "pre_row = None\n",
    "every_trade_ls = list()\n",
    "contract_multiplier = 5\n",
    "\n",
    "price_ceil = 30000   # 可尝试动态获取更新\n",
    "price_ground = 20000\n",
    "pass\n",
    "\n",
    "for ind, row in enumerate(test_data_values[:1000]):\n",
    "    \n",
    "    # 跳过第一条数据\n",
    "    if pre_row is None:\n",
    "        continue\n",
    "\n",
    "    # 如果该tick发生交易, VolumeTrade != 0 \n",
    "    if row[TickVolumeTrade_ind]:\n",
    "        # 当前 tick 存在前 tick 且 前一个tick 不是 空白行\n",
    "        if pre_row[0] is not np.nan:\n",
    "            temp_Date = row[Date_ind]\n",
    "            temp_Time = row[Time_ind]\n",
    "            temp_Timestamp = row[Timestamp_ind]\n",
    "            \n",
    "            temp_LastPx = row[LastPx_ind]\n",
    "            temp_TickVolumeTrade = row[TickVolumeTrade_ind]\n",
    "            temp_TickValueTrade = row[TickValueTrade_ind]\n",
    "            \n",
    "            # 首先确定方向 direction\n",
    "            if (temp_LastPx >= pre_row[AskPx1_ind] or \n",
    "                (row[AskPx1_ind] > pre_row[AskPx1_ind]) or \n",
    "               (row[AskVol1_ind] < pre_row[AskVol1_ind])):\n",
    "                direction = \"B\"\n",
    "            elif (temp_LastPx <= pre_row[BidPx1_ind] or \n",
    "                  (row[BidPx1_ind] < pre_row[BidPx1_ind]) or\n",
    "                  (row[BidVol1_ind] < pre_row[BidVol1_ind])):\n",
    "                direction = \"S\"\n",
    "            # 买方前两档买量 大于 卖方前两档卖量 （买压 > 卖压）\n",
    "            elif pre_row[BidVol1_ind: BidVol1_ind+2] > pre_row[AskVol1_ind-2: AskVol1_ind]:\n",
    "                direction = \"B\"\n",
    "            elif pre_row[BidVol1_ind: BidVol1_ind+2] < pre_row[AskVol1_ind-2: AskVol1_ind]:\n",
    "                direction = \"S\"\n",
    "            else:\n",
    "                if random.random() > 0.5:\n",
    "                    diretion = \"B\"\n",
    "                else:\n",
    "                    direction = \"S\"\n",
    "            \n",
    "            split_trade_prices = list()\n",
    "            split_trade_values = list()\n",
    "            temp_price = temp_LastPx\n",
    "            #　将tick数据进行拆分，变为逐笔数据\n",
    "            if temp_TickVolumeTrade == 2:\n",
    "                # 获取成交价格 和 交易额\n",
    "                split_trade_prices.append(temp_price)\n",
    "                temp_value = temp_price*1*5\n",
    "                split_trade_values.append(temp_values)\n",
    "                temp_TickValueTrade -= temp_value\n",
    "                split_trade_values.append(temp_TickValueTrade)\n",
    "                split_trade_prices.append(temp_TickValueTrade * 0.2)\n",
    "            \n",
    "            if temp_TickVolumeTrade == 4:\n",
    "                \"\"\"\n",
    "                成交四笔的情形，大部分 AskVolume1=2或者BidVolume1=2\n",
    "                \"\"\"\n",
    "                # 特殊情形，AskVolume1=2或者BidVolume1=2\n",
    "                if pre_row[AskVol1_ind] == 2 or pre_row[BidVol1_ind] == 2:\n",
    "                    split_trade_prices.extend([temp_price, temp_price])\n",
    "                    temp_value = temp_price*1*5\n",
    "                    split_trade_values.extend([temp_value, temp_value])\n",
    "                    temp_TickValueTrade -= temp_value*2\n",
    "                    # 如果可以整除，则简化，直接用中间价作为成交价估计\n",
    "                    # 当然此处可加入与 order_book 量价变化进行轮询、对比、运算，更准确地确定成交价\n",
    "                    if temp_TickValueTrade % 10 == 0:\n",
    "                        temp_price = temp_TickValueTrade * 0.1\n",
    "                        temp_value = temp_price * 1 * 5\n",
    "                        split_trade_prices.extend([temp_price, temp_price])\n",
    "                        split_trade_values.extend([temp_value, temp_value])\n",
    "                    else:\n",
    "                        middle_price = temp_TickValueTrade * 0.1\n",
    "                        while middle_price % 5 != 0.0:  \n",
    "                            middle_price += 2.5   # 2.5 由 contract_multiplier 决定\n",
    "                        split_trade_prices.extend([middle_price, temp_TickValueTrade * 0.2 - middle_price])\n",
    "                        split_trade_values.extend([middle_price * 5, temp_TickValueTrade - middle_price * 5])\n",
    "                # 一般性通用情况\n",
    "                else:\n",
    "                    while temp_TickValueTrade != 0:\n",
    "                        # 第一个单子\n",
    "                        split_trade_prices.append(temp_price)\n",
    "                        temp_value = temp_price * 5\n",
    "                        split_trade_values.append(temp_value)\n",
    "                        temp_TickValueTrade -= temp_value\n",
    "                        \n",
    "                        temp_BidPx1 = pre_row[BidPx1_ind]\n",
    "                        temp_BidPx2 = pre_row[BidPx1_ind+1]\n",
    "                        temp_AskPx1 = pre_row[AskPx1_ind]\n",
    "                        temp_AskPx2 = pre_row[AskPx1_ind-1]\n",
    "                        \n",
    "                        # 还剩下三个成交单\n",
    "                        # 该价格组合部分可以参考背包问题中动态规划思想\n",
    "                        if direction == \"S\":\n",
    "                            # 列出所有可能价格，进行排列组合，找出距离 LastPrice 最近的 三个价格\n",
    "                            # 如果买一卖一、买一买二距离过大，则缩小，以便下一步排列组合减少搜索复杂度\n",
    "                            if temp_AskPx1 - temp_BidPx1 > 100:\n",
    "                                temp_AskPx1 = temp_BidPx1 + 100\n",
    "                            if temp_BidPx1 - temp_BidPx2 > 100:\n",
    "                                temp_BidPx2 = temp_BidPx1 - 100\n",
    "                                \n",
    "                            price_list = list(itertools.combinations(list(range(temp_BidPx2, temp_AskPx1, contract_multiplier)), 3))\n",
    "                            price_list.extend([[each]*3 for each in range(temp_BidPx2, temp_AskPx1, contract_multiplier)])\n",
    "                            price_array = np.array(price_list)\n",
    "                            # 把组合价值不正确的剔除\n",
    "                            price_array = price_array[(price_array * 5).sum(axis=1) == temp_TickValueTrade]\n",
    "                            \n",
    "                            temp_max = 0\n",
    "                            temp_price = list()\n",
    "                            for each in price_array:\n",
    "                                temp = np.sum(np.square((each - temp_BidPx1)))\n",
    "                                if temp > temp_max:\n",
    "                                    temp_max = temp\n",
    "                                    temp_price = each\n",
    "                        if direction == \"B\":\n",
    "                            # 列出所有可能价格，进行排列组合，找出距离 LastPrice 最近的 三个价格\n",
    "                            # 如果卖一卖二、卖一买一距离过大，则缩小，以便下一步排列组合减少搜索复杂度\n",
    "                            if temp_AskPx2 - temp_AskPx1 > 100:\n",
    "                                temp_AskPx2 = temp_AskPx1 + 100\n",
    "                            if temp_AskPx1 - temp_BidPx1 > 100:\n",
    "                                temp_BidPx1 = temp_AskPx1 - 100\n",
    "                            price_list = list(itertools.combinations(list(range(temp_BidPx1, temp_AskPx2, contract_multiplier)), 3))\n",
    "                            price_list.extend([[each]*3 for each in range(temp_BidPx1, temp_AskPx2, contract_multiplier)])\n",
    "                            price_array = np.array(price_list)\n",
    "                            # 把组合价值不正确的剔除\n",
    "                            price_array = price_array[(price_array * 5).sum(axis=1) == temp_TickValueTrade]\n",
    "                            \n",
    "                            temp_max = 0\n",
    "                            temp_price = list()\n",
    "                            for each in price_array:\n",
    "                                temp = np.sum(np.square((each - temp_BidPx1)))\n",
    "                                if temp > temp_max:\n",
    "                                    temp_max = temp\n",
    "                                    temp_price = each\n",
    "                    \n",
    "            if temp_TickVolumeTrade == 6:\n",
    "                pass\n",
    "            if temp_TickVolumeTrade == 10:\n",
    "                pass\n",
    "            if temp_TickVolumeTrade == 12:\n",
    "                pass\n",
    "            \n",
    "\n",
    "            while temp_TickValueTrade > price_ceil:\n",
    "                \n",
    "                pass\n",
    "            else:\n",
    "                if temp_TickValueTrade:\n",
    "                    split_trade_values.append(temp_TickValueTrade)\n",
    "                \n",
    "    pre_row = row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 10\n",
    "ls = list()\n",
    "while a > 5:\n",
    "    ls.append(1)\n",
    "    a -= 1\n",
    "else:\n",
    "    while a > 0:\n",
    "        ls.append(2)\n",
    "        a -= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_dict = defaultdict(int)\n",
    "prices = [123, 123, 100, 121, 100, 111, 110]\n",
    "for each in prices:\n",
    "    test_dict[each] += 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defaultdict(int, {123: 2, 100: 2, 121: 1, 111: 1, 110: 1})"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values([2, 2, 1, 1, 1])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_dict.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([(123, 2), (100, 2), (121, 1), (111, 1), (110, 1)])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_dict.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "banji=['三年级11班', '三年级12班', '三年级13班', '三年级14班']\n",
    "bannum=[72, 71, 73, 75]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "#列表转化为array矩阵\n",
    "arr1=numpy.array(banji)                            #1行1列\n",
    "arr2=numpy.array(bannum)                           #1行1列 可用.T转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "纵向方法1 [['三年级11班' '三年级12班' '三年级13班' '三年级14班']\n",
      " ['72' '71' '73' '75']]\n",
      "**************************************************\n",
      "纵向方法2 ['三年级11班' '三年级12班' '三年级13班' '三年级14班' '72' '71' '73' '75']\n"
     ]
    }
   ],
   "source": [
    "#数组纵向合并\n",
    "print('纵向方法1',numpy.vstack((arr1,arr2)))       #1行2列\n",
    "print(\"*\"*50)\n",
    "print('纵向方法2',numpy.r_[arr1,arr2])             #1行1列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr=numpy.c_[arr1,arr2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['三年级11班', '72'],\n",
       "       ['三年级12班', '71'],\n",
       "       ['三年级13班', '73'],\n",
       "       ['三年级14班', '75']], dtype='<U11')"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------------------------------------------- dataframe\n",
      "        0   1\n",
      "0  三年级11班  72\n",
      "1  三年级12班  71\n",
      "2  三年级13班  73\n",
      "3  三年级14班  75\n"
     ]
    }
   ],
   "source": [
    "print(\"-\"*50,\"dataframe\")\n",
    "#arr矩阵转为dataframe\n",
    "bframe=pd.DataFrame(arr)\n",
    "print(bframe)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "banji=['三年级11班', '三年级12班', '三年级13班', '三年级14班', '三年级1班', '三年级3班', '三年级4班', '三年级5班', '三年级6班(2020届理科6班)', '三年级7班', '三年级8班', '三年级9班', '二年级10班', '二年级11班', '二年级12班', '二年级13班', '二年级14班', '二年级15班', '二年级1班', '二年级24班(测试)', '二年级2班', '二年级3班', '二年级4班', '二年级5班', '二年级6班', '二年级7班', '二年级8班', '二年级9班', '宋基文复(1)班', '宋基理复(1)班', '宋基理复(2)班', '宋基高一文(1)班', '宋基高一文(2)班', '宋基高一理(1)班', '宋基高一理(2)班', '宋基高二文(1)班', '宋基高二理(1)班', '宋基高二理(2)班', '校部高一年级1班', '校部高一年级2班', '校部高一年级3班', '校部高一年级4班', '校部高一年级5班', '校部高一年级6班', '校部高一年级7班', '校部高一年级8班', '校部高一年级文1班', '校部高一年级文2班', '校部高一年级文3班', '校部高一年级文4班', '高一（新校）10班', '高一（新校）11班', '高一（新校）12班(文)', '高一（新校）1班', '高一（新校）2班(文)', '高一（新校）3班', '高一（新校）4班(文)', '高一（新校）5班', '高一（新校）6班', '高一（新校）7班', '高一（新校）8班', '高一（新校）9班(文)', '高三文1', '高三文2班', '高三文3', '高三文4', '高三文5', '高三文6', '高三文7', '高三文8', '高三理2班', '高三理科10班', '高二文1', '高二文2', '高二文3', '高二文4', '高二文5', '高二文6', '高二文7', '高二文8班']\n",
    "bannum=[72, 71, 73, 75, 50, 46, 69, 73, 75, 73, 76, 76, 75, 75, 71, 73, 72, 77, 30, 2, 30, 73, 74, 71, 41, 71, 74, 70, 40, 45, 46, 55, 52, 53, 55, 66, 47, 46, 45, 44, 63, 64, 63, 60, 60, 60, 50, 49, 50, 54, 69, 65, 58, 63, 52, 67, 52, 65, 85, 51, 60, 62, 60, 60, 67, 67, 70, 68, 69, 66, 66, 77, 79, 89, 127, 125, 122, 118, 156, 77]\n",
    "\n",
    "\n",
    "#列表转化为array矩阵\n",
    "arr1=numpy.array(banji)                            #1行1列\n",
    "arr2=numpy.array(bannum)                           #1行1列 可用.T转置\n",
    "\n",
    "print(arr1,\"#\"*50,\"\\n\",arr2)\n",
    "\n",
    "#数组纵向合并\n",
    "print('纵向方法1',numpy.vstack((arr1,arr2)))       #1行2列\n",
    "print(\"*\"*50)\n",
    "print('纵向方法2',numpy.r_[arr1,arr2])             #1行1列\n",
    "\n",
    "#数组横向合并\n",
    "print(\"-\"*50)\n",
    "print('横向方法1',numpy.hstack((arr1,arr2)))       #1行1列\n",
    "print(\"*\"*50)\n",
    "print('横向方法2',numpy.c_[arr1,arr2])             #1行，内部2个列表\n",
    "\n",
    "\n",
    "arr=numpy.c_[arr1,arr2]\n",
    "\n",
    "print(\"-\"*50,\"dataframe\")\n",
    "#arr矩阵转为dataframe\n",
    "bframe=pd.DataFrame(arr)\n",
    "print(bframe)\n",
    "\n",
    "#dataframe转为arr矩阵\n",
    "brrnumpy=bframe.to_numpy()\n",
    "print(\"-\"*50,\"numpy矩阵\")\n",
    "print(brrnumpy)\n",
    "\n",
    "brr=bframe.values\n",
    "print(\"-\"*50,\"直接读dataframe值\")\n",
    "print(brr)\n",
    "\n",
    "\n",
    "#dataframe取某列转为是series,list\n",
    "banji_ser=bframe[0]                            # dataframe  0    1\n",
    "                                                # 0   三年级11班   72\n",
    "                                                # 1   三年级12班   71\n",
    "                                                # 2   三年级13班   73\n",
    "num_ser=bframe[1]\n",
    "\n",
    "\n",
    "# series 转换为list\n",
    "banji_list=banji_ser.tolist()\n",
    "print(\"-\"*50,\"\\n  班级列表\")\n",
    "print(banji_list)\n",
    "\n",
    "\n",
    "num_list1=bframe[1].values.tolist()   #bframe['a']\n",
    "print('Frame --》list',num_list1)\n",
    "\n",
    "num_list2=bframe[1].tolist()\n",
    "print('Frame --》list',num_list2)\n",
    "\n",
    "\n",
    "num_list3=bframe[1].drop_duplicates().values.tolist()\n",
    "print('去重Frame --》list',num_list3)\n",
    "\n",
    "num_list4=list(set(bframe[1])) # 数字\n",
    "print('去重Frame --》list',num_list4)\n",
    "\n",
    "# 转换整个到list \n",
    "num_list5= bframe.values.tolist()\n",
    "print('整体Frame --》list',num_list5)\n",
    "\n",
    "#arr转为list  \n",
    "arr_list=arr.tolist()\n",
    "print(\"-\"*50,\"\\n  班级列表\")\n",
    "print(arr_list)\n",
    "\n",
    "#多行多列的arr先分割再转化\n",
    "arr1=numpy.hsplit(arr,2)[0]                     #三年级11班   72 平均分两列 为[0] [1] 如果分割大于内容数，出错\n",
    "arr_list1=arr1.tolist()\n",
    "print(\"-\"*50,\"\\n  水平分割班级列表\")\n",
    "print(arr_list1)\n",
    "\n",
    "#在垂直方向分割成三行\n",
    "arr2=numpy.vsplit(arr,2)[0]                     #不能平均分割时也出错\n",
    "arr_list2=arr2.tolist()\n",
    "print(\"-\"*50,\"\\n  垂真分割班级列表\")\n",
    "print(arr_list2)\n",
    "\n",
    "\n",
    "\n",
    "#list转字符串\n",
    "str_arr=\" \".join(banji_list)                      #不能转化多列的，只能转化一列的[[11]，[12]]也不行\n",
    "print(\"-\"*50,\"\\n  班级字符串\")                     #如转换arr_list1错，arrlist2出错，由dataframe转出的单列可以\n",
    "print(str_arr)\n",
    "\n",
    "#字符串转list\n",
    "str_list=str_arr.split()\n",
    "print(\"-\"*50,\"\\n  班级列表\")\n",
    "print(str_list)\n",
    "\n",
    "#list转series\n",
    "ser1=pd.Series(banji_list)\n",
    "print(ser1)\n",
    "ser2=pd.Series(banji_list,index=range(len(banji_list)))\n",
    "print(ser2)\n",
    "\n",
    "#字典转为DataFrame\n",
    "pd.DataFrame({'数目':999},index=[0])\n",
    "#字典放在list再转为DataFrame\n",
    "pd.DataFrame([{'数目1':999},{'数目2':888}])\n",
    "\n",
    "#list列表不能直接转为DataFrame,先转为字典，再字典转为DataFrame\n",
    "dict_c={\"班级\":banji,\n",
    "\t\t\"人数\":bannum}\n",
    "print(pd.DataFrame(dict_c))\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
